package com.aloda.controller.admin;

import com.aloda.anno.OpLog;
import com.aloda.dto.UserAddDTO;
import com.aloda.dto.UserEditDTO;
import com.aloda.dto.UserPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.UserService;
import com.aloda.vo.UserEditVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminUserController")
@Slf4j
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户
     * @param userPageDTO
     * @return
     */
    @PostMapping("/list")
    public Result page(@RequestBody UserPageDTO userPageDTO){
        log.info("后台用户分页查询：{}",userPageDTO);
        PageResult pageResult = userService.pageQuery(userPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 后台新增用户
     * @param userAddDTO
     * @return
     */
    @OpLog
    @PostMapping("/add")
    public Result addUser(@RequestBody UserAddDTO userAddDTO){
        log.info("后台新增用户：{}",userAddDTO);
        userService.addUser(userAddDTO);
        return Result.success();
    }

    /**
     * 编辑用户信息回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserEditVO> getUser(@PathVariable Long id){
        log.info("编辑用户{}信息回显",id);
        UserEditVO userEditVO = userService.getUserById(id);
        return Result.success(userEditVO);
    }

    /**
     * 更新用户信息
     * @param userEditDTO
     * @return
     */
    @OpLog
    @PutMapping("/update")
    public Result editUser(@RequestBody UserEditDTO userEditDTO){
        log.info("更新用户信息：{}",userEditDTO);
        userService.updateUser(userEditDTO);
        return Result.success();
    }

    /**
     * 用户权限设置
     * @param id
     * @param auth
     * @return
     */
    @OpLog
    @PutMapping("/logout/{id}/{auth}")
    public Result logout(@PathVariable Long id,@PathVariable Integer auth){
        log.info("用户{}设置权限{}",id,auth);
        userService.setAuth(id,auth);
        return Result.success();
    }

    /**
     * 批量注销用户
     * @param ids
     * @return
     */
    @PutMapping("/logouts")
    public Result logouts(@RequestBody List<Long> ids){
        log.info("注销用户{}",ids);
        userService.logoutUser(ids);
        return Result.success();
    }


}
